package com.chanyachting.emall.action.activities;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.domain.Newsactivity;
import com.chanyachting.emall.form.NewsActivityForm;
import com.chanyachting.emall.service.ChannelService;
import com.chanyachting.emall.service.NewsActivityService;
import com.chanyachting.emall.serviceImpl.ChannelServiceImpl;
import com.chanyachting.emall.serviceImpl.NewsActivityServiceImpl;

public class QueryOneNewsAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		NewsActivityForm naForm = (NewsActivityForm) form;
		NewsActivityService naservice=new NewsActivityServiceImpl();
		ChannelService cService = new ChannelServiceImpl();
	
		int id = naForm.getId();
		
		Newsactivity news = naservice.getOneNewsById(String.valueOf(id));
		
		if (news == null) {
			return mapping.findForward("error");
		}
		
		naForm.setNews(news);
		int channelId = news.getChannelId();
		int subChannelId = news.getSubChannelId();
		
		Channel c = cService.getOneChannel(new Channel(channelId));
		if (c == null) {
			return mapping.findForward("error");
		}
		String channelName = c.getName();
		
		List<Channel> subChannelList = cService.getSubChannel(c);
		
		if (subChannelList == null || subChannelList.size() == 0) {
			return mapping.findForward("error");
		}
		//判断指定的sub频道是否存在
		boolean isExistSubChannel = false;
		for(Channel cn:subChannelList) {
			if (cn == null || cn.getId() == null) {
				continue;
			}
			if (cn.getId().equals(subChannelId)) {
				naForm.setSubChannelName(cn.getName());
				isExistSubChannel = true;
				break;
			}
		}
		if (isExistSubChannel == false) {
			return mapping.findForward("error");
		}
		naForm.setChannelId(channelId);
		naForm.setSubChannelId(subChannelId);
		naForm.setChannelName(channelName);
		
		if (news.getImgUrl() == null || "".equals(news.getImgUrl())) {
			return mapping.findForward("newsActivityNoticeOneNews1");
		}
		return mapping.findForward("newsActivityNoticeOneNews2");
	}
}
